var socketio = require('socket.io')
var io
var guestNumber = 1
var nickNames = {}
var namesUsed = {}
var currentRoom = {}

exports.listen = function (server) {
    io = socketio.listen(server)
    io.set('log level', 1)

    io.sockets.on('connection', function (socket) {
        guestNumber = assignGuestName(socket, guestNumber, nickNames, namesUsed)

        joinRoom(socket, 'Lobby')

        handleMessageBroadcasting(socket, nickNames)

        handleNameChangeAttempts(socket, nickNames, namesUsed)

        handleRoomJoining(socket)

        socket.on('rooms',  function () {
            socket.emit('rooms', io.sockets.manager.room)
        })

        handleClientDisconnection(socket, nickNames, namesUsed)

    })
}

function assignGuestName(socket, guestNumber, nickNames, namesUsed) {
    var name = 'Guest' + guestNumber;
    nickNames[socket.id] = name      
    socket.emit('nameResult', {
        success: true,
        name: name
    })              
    namesUsed.push(name)
    return guestNumber + 1
}
function joinRoom (socket, room) {
    socket.join(room)
    currentRoom[socket.id] = room
    socket.emit('joinResult', {room: room})
    socket.broadcast.to(room).emit('message', {
        text: nickNames[socket.id] + '加入' + room
    })
    var usersRoom = io.sockets.clients(room)
    if (usersInRoom.length ) {
        var usersInRoomSummary = 'Users currently in' + room + ':'
        var userSocketId = usersInRoom[index.id]
        if (userSocketId != socket.id) {
            if (index > 0) {
                usersInRoomSummary += ', '
            }
            usersInRoomSummary += nickNames[userSocketId]
        }
    }
    usersInRoomSummary += '.'
    socket.emit('message', {text: usersInRoomSummary})
}
function handleNameChangeAttempts (socket, nickNames, namesUsed) {
    socket.on('nameAttempt', function () {
        if (name.indexOf('Guest') == 0) {
            socket.emit('nameResult', {
                success: false,
                message: "Names cannot begin with Guest"
            })
        } else {
            if (namesUsed.indexOf(name) == -1) {
                var previousName = nickNames[socket.id]
                var previousNameIndex = namesUsed.indexOf(previousName)
                namesUsed.push(name)
                delete namesUsed[previousNameIndex]

                socket.emit('nameResult', {
                    success: true,
                    name: name
                })
                socket.broadcast.to(currentRoom[socket.id]).emit('message', {
                    text: previousName + 'is now known as ' + name + '.'
                })
            } else {
                socket.emit('nameResult', {
                    success: false,
                    message: 'That name is already in use'
                })
            }
            
        }
    })
}
function handleRoomJoining(socket) {
    // 
    socket.on('message', function (message) {
        socket.broadcast.to(message.room).emit('message', {
            text: nickNames[socket.id]  + ':' + message.text
        })
    }) 
    // 更换房间
    socket.on('join', function (room) {
        socket.leave(currentRoom[socket.id])
        joinRoom(socket, room.newRoom)
    })
}
// 离开程序，移除用户的昵称
function hangleClientDisconnection () {
    socket.on('disconnect', function () {
        var nameIndex = namesUsed.indexOf(nickNames[socket.id])
        delete namesUsed[nameIndex]
        delete nickNames[socket.id]
    })
}
